﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.Plumbing
{
	public partial class FormLayWaterTrank : System.Windows.Forms.Form
	{
		public FormLayWaterTrank(Autodesk.Revit.DB.Document doc)
		{
			this.listInfos = this.sfdr.GetAllNameAndImage("EquipmentWaterTank");
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Location = new System.Drawing.Point(100, 100);
			base.Icon = Resource.YArchitechIcon;
			this.initParameterSetting();
			this.InitCombox();
			if (this.listInfos.Count > 0)
			{
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.GetParasFromDB(listItem);
						this.pictureBox1.Image = Image.FromFile(listItem.Value);
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
		}

		private void InitCombox()
		{
			string[] items = new string[]
			{
				"50",
				"70",
				"80",
				"100",
				"150"
			};
			this.cbInpipe.Items.AddRange(items);
			string[] items2 = new string[]
			{
				"70",
				"80",
				"100",
				"150",
				"200"
			};
			this.cbOutpipe.Items.AddRange(items2);
			string[] items3 = new string[]
			{
				"70",
				"80",
				"100",
				"150",
				"200"
			};
			this.cbOverflowpipe.Items.AddRange(items3);
			string[] items4 = new string[]
			{
				"40",
				"50",
				"70",
				"80"
			};
			this.cbDrainpipe.Items.AddRange(items4);
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			FormChooseWaterTrank formChooseWaterTrank = new FormChooseWaterTrank(this.name, this.listInfos);
			formChooseWaterTrank.Owner = this;
			if (formChooseWaterTrank.ShowDialog() == DialogResult.OK && this.name != formChooseWaterTrank.ImageName && formChooseWaterTrank.ImageName != "")
			{
				this.name = formChooseWaterTrank.ImageName;
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.pictureBox1.Image = Image.FromFile(listItem.Value);
						this.GetParasFromDB(listItem);
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
		}

		private void GetParasFromDB(ListItem item)
		{
			List<ListItem> infoById = this.sfdr.GetInfoById("EquipmentWaterTank", int.Parse(item.Tag.ToString()));
			if (infoById.Count > 0)
			{
				foreach (ListItem listItem in infoById)
				{
					string text = listItem.Name;
					uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(text);
					if (num <= 1476696200u)
					{
						if (num != 106886956u)
						{
							if (num != 1329497866u)
							{
								if (num == 1476696200u)
								{
									if (text == "PipeOverflowDiameter")
									{
										this.cbOverflowpipe.Text = listItem.Value.ToString();
									}
								}
							}
							else if (text == "PipeDrainDiameter")
							{
								this.cbDrainpipe.Text = listItem.Value.ToString();
							}
						}
						else if (text == "PipeOutDiameter")
						{
							this.cbOutpipe.Text = listItem.Value.ToString();
						}
					}
					else if (num <= 1946647915u)
					{
						if (num != 1485985708u)
						{
							if (num == 1946647915u)
							{
								if (text == "WaterTankHeight")
								{
									this.wtHeigth.Text = listItem.Value.ToString();
								}
							}
						}
						else if (text == "WaterTankLength")
						{
							this.wtLength.Text = listItem.Value.ToString();
						}
					}
					else if (num != 3111219964u)
					{
						if (num == 4194422953u)
						{
							if (text == "PipeInDiameter")
							{
								this.cbInpipe.Text = listItem.Value.ToString();
							}
						}
					}
					else if (text == "WaterTankWidth")
					{
						this.wtWidth.Text = listItem.Value.ToString();
					}
				}
			}
		}

		private void SaveParasFofDB()
		{
			List<ListItem> list = new List<ListItem>();
			list.Add(new ListItem("WaterTankLength", this.watertankParam.Length.ToString()));
			list.Add(new ListItem("WaterTankWidth", this.watertankParam.Width.ToString()));
			list.Add(new ListItem("WaterTankHeight", this.watertankParam.Heigth.ToString()));
			list.Add(new ListItem("PipeInDiameter", this.watertankParam.Inpipe.ToString()));
			list.Add(new ListItem("PipeOutDiameter", this.watertankParam.Outpipe.ToString()));
			list.Add(new ListItem("PipeOverflowDiameter", this.watertankParam.Overpipe.ToString()));
			list.Add(new ListItem("PipeDrainDiameter", this.watertankParam.Drainpipe.ToString()));
			if (!this.sfdr.SetInfoById("EquipmentWaterTank", this.id, list))
			{
				YJKMessageBox.Show("保存数据失败");
			}
		}

		public bool GetWaterTankParamFromForm()
		{
			List<System.Windows.Forms.Control> list = new List<System.Windows.Forms.Control>();
			list.Add(this.wtWidth);
			list.Add(this.wtLength);
			list.Add(this.wtHeigth);
			list.Add(this.cbInpipe);
			list.Add(this.cbOutpipe);
			list.Add(this.cbOverflowpipe);
			list.Add(this.cbDrainpipe);
			if (ControlJudgement.IsAllControlEmpty(list) || !(this.tbLevel.Text.Trim() != ""))
			{
				return false;
			}
			if (!ControlJudgement.IsAllControlNumber(list))
			{
				YJKMessageBox.Show("参数都必须为正数");
				return false;
			}
			if (double.Parse(this.wtWidth.Text.Trim()) > 100000.0 || double.Parse(this.wtLength.Text.Trim()) > 100000.0 || double.Parse(this.wtHeigth.Text.Trim()) > 100000.0)
			{
				YJKMessageBox.Show("水箱的长宽高均不能超过100000mm");
				return false;
			}
			this.watertankParam = new WatertankParam();
			this.watertankParam.Width = double.Parse(this.wtWidth.Text.Trim());
			this.watertankParam.Length = double.Parse(this.wtLength.Text.Trim());
			this.watertankParam.Heigth = double.Parse(this.wtHeigth.Text.Trim());
			this.watertankParam.Inpipe = double.Parse(this.cbInpipe.Text.Trim());
			this.watertankParam.Outpipe = double.Parse(this.cbOutpipe.Text.Trim());
			this.watertankParam.Overpipe = double.Parse(this.cbOverflowpipe.Text.Trim());
			this.watertankParam.Drainpipe = double.Parse(this.cbDrainpipe.Text.Trim());
			this.watertankParam.Offset = double.Parse(this.tbLevel.Text.Trim());
			return true;
		}

		private void initParameterSetting()
		{
			this.tbLevel.Text = this.userSetting.GetValue("watertanks", "offset");
			this.name = this.userSetting.GetValue("watertanks", "name");
		}

		private void UserSettingWrite()
		{
			try
			{
				this.userSetting.WriteValue("watertanks", "offset", this.tbLevel.Text);
				this.userSetting.WriteValue("watertanks", "name", this.name);
				this.SaveParasFofDB();
			}
			catch (Exception)
			{
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.GetWaterTankParamFromForm())
			{
				this.UserSettingWrite();
				base.DialogResult = DialogResult.OK;
			}
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			if (FamilyUtil.PathIni.ReadPath(this.doc.Application.VersionNumber) == "")
			{
				YJKMessageBox.Show("请先安装盈建科族立得。");
				return;
			}
			LayFamilyForm layFamilyForm = new LayFamilyForm(new PlumbingDataReader(this.doc).GetFamilyAllInfoTree("水箱"), this.doc);
			if (layFamilyForm.ShowDialog() == DialogResult.OK)
			{
				this.fs = layFamilyForm.fs;
				this.watertankParam = new WatertankParam();
				this.watertankParam.Offset = layFamilyForm.OffSet;
				base.DialogResult = DialogResult.OK;
			}
		}

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private List<ListItem> listInfos = new List<ListItem>();

		private UserSetting userSetting = new UserSetting();

		public WatertankParam watertankParam;

		private string name = "";

		public int id;

		public FamilySymbol fs;

		private Document doc;
	}
}
